package com.wyy.ems_vue.service.impl;/*
 **
 * @author yangyang
 *@date 2022/07/02 22:50
 **/

import com.wyy.ems_vue.dao.EmpDao;
import com.wyy.ems_vue.entity.Emp;
import com.wyy.ems_vue.entity.User;
import com.wyy.ems_vue.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(propagation = Propagation.SUPPORTS)
@Slf4j
@CacheConfig(cacheNames = {"myCache"})
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao empDao;
    @Cacheable(key = "'findAll'")
    @Override
    public List<Emp> findAll() {
        List<Emp> list = empDao.findAll();
        return list;
    }
    //@CachePut缓存新增的或更新的数据到缓存，其中缓存的名称为people，数据的key是person的id
    @CachePut(key = "#emp.id")
    // @CacheEvict从缓存中删除key为参数userList的数据
    @CacheEvict(key = "'findAll'")
    @Override
    public void save(Emp emp) {
      empDao.save(emp);
    }
    @CacheEvict(value = "emp",key = "#id")
    @Override
    public void delete(String id) {
        empDao.delete(id);
    }
    @Cacheable(key = "#id")
    @Override
    public Emp findOne(String id) {
        Emp one = empDao.findOne(id);
        return one;
    }
    @CachePut(key = "#emp.id")
    @Override
    public void update(Emp emp) {
      empDao.update(emp);
    }
}
